//
//  USAlbumStyleView.m
//  us
//
//  Created by karry on 2025/4/26.
//

#import "USAlbumStyleView.h"
#import "USAlbumStyleCell.h"

@interface USAlbumStyleView () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSArray *styleGroups;


@property (nonatomic, strong) UIButton *closeButton;

@end

@implementation USAlbumStyleView


- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self setupCollectionView];
        [self initDefaultStickerGroups];

    }
    return self;
}

///使用默认贴纸
- (void)initDefaultStickerGroups{
    self.styleGroups = @[@"album_style_0",@"album_style_1"];
    
    [self.collectionView reloadData];
}

- (void)styleAppear{
    if (self.hidden) {
        self.hidden = NO;
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_offset(0);
        }];
        [UIView animateWithDuration:0.3 animations:^{
            [self.superview layoutIfNeeded];
        }completion:^(BOOL finished) {

        }];
    }else {
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_offset(self.jk_height);
        }];
        
        [UIView animateWithDuration:0.3 animations:^{
            [self.superview layoutIfNeeded];
        }completion:^(BOOL finished) {
            self.hidden = YES;
        }];
    }
}
#pragma mark - Setup

- (void)setupCollectionView {
    self.backgroundColor = UIColor.mi_1_color;
    self.layer.cornerRadius = 14;
    self.layer.masksToBounds = YES;
    
    UIView *toolView = [[UIView alloc]init];
    [self addSubview:toolView];
    [toolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.height.mas_equalTo(44);
    }];
    
    self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.closeButton setImage:[UIImage imageNamed:@"normal_close"] forState:0];
    [self.closeButton addTarget:self action:@selector(closeButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [toolView addSubview:self.closeButton];
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(toolView).offset(16);
        make.centerY.equalTo(toolView);
        make.width.height.mas_equalTo(28);
    }];
    
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = UIColor.mi_3_color.CGColor;
    shapeLayer.lineWidth = 1.2;
    shapeLayer.lineCap = kCALineCapRound;
    shapeLayer.lineDashPattern = @[@8, @8];

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(16, 43)];
    [path addLineToPoint:CGPointMake(ScreenWidth - 32, 44)];
    
    shapeLayer.path = path.CGPath;
    [self.layer addSublayer:shapeLayer];
    
    UIView *collectionViewContainer = [[UIView alloc] init];
    [self addSubview:collectionViewContainer];
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:({
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = CGSizeMake(96, 118);
        layout.minimumLineSpacing = 16;
        layout.sectionInset = UIEdgeInsetsMake(0, 22, 0, 22);
        layout;
    })];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.backgroundColor = UIColor.clearColor;;
    [self.collectionView registerClass:[USAlbumStyleCell class] forCellWithReuseIdentifier:@"USAlbumStyleCell"];
    [self addSubview:self.collectionView];

    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(toolView.mas_bottom).offset(1);
        make.left.bottom.right.equalTo(self);
    }];
}

-(void)closeButtonTapped{
    [self styleAppear];
}

#pragma mark - collectionView CollectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.styleGroups.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    USAlbumStyleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"USAlbumStyleCell" forIndexPath:indexPath];
    

    cell.imgView.image = [UIImage imageNamed:self.styleGroups[indexPath.row]];
    return cell;
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [USFeedbackTool impactMedium];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(albumStyleViewdidSelectStyle:)]) {
        [self.delegate albumStyleViewdidSelectStyle:indexPath.row];
    }
}


@end
